package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

@Service(interfaceClass = MemberService.class)
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public Member findByTelephone(String telephone) {
        return memberMapper.findByTelephone(telephone);
    }

    @Override
    public void add(Member member) {
        memberMapper.add(member);
    }

    @Override
    public Map getMemberReport() throws Exception {


        //实例化日历对象
        Calendar calendar = Calendar.getInstance();
        //将日历对象的当前时间往前推12个月
        calendar.add(Calendar.MONTH,-12);


        //定义一个list用来封装每一个月的月份
        List<String> memberMonths = new ArrayList<>();

        //定义一个list用来封装每一个月的会员数量
        List<Integer> memberCounts = new ArrayList<>();

        //循环遍历，获取过去的十二个月
        for (int i = 0; i < 12; i++) {
            Date time = calendar.getTime();
            String date = DateUtils.parseDate2String(time,"yyyy-MM");

            memberMonths.add(date);

            //定义每个月查询的起始值
            String dateBegin = date + "-01";
            //定义每个月的结束值
            String dateEnd = date + "-31";

            //查询每个月的会员数量
            Integer memberCount = memberMapper.getMemberReport(dateBegin,dateEnd);

            memberCounts.add(memberCount);

            calendar.add(Calendar.MONTH,1);
        }

        //将查询出来的数据封装到map中
        Map<String, List> map = new HashMap<>();

        map.put("memberMonths",memberMonths);
        map.put("memberCounts",memberCounts);

        return map;
    }

    //查询男女会员数量
    @Override
    public List<Map> getMemberGenderCount() {

        int man = memberMapper.findMemberGenderCount(1);
        int woman = memberMapper.findMemberGenderCount(2);

        Map map1 = new HashMap<>();
        map1.put("name", "男");
        map1.put("value", man);

        Map map2 = new HashMap<>();
        map2.put("name", "女");
        map2.put("value", woman);

        List<Map> list = new ArrayList<>();
        list.add(map1);
        list.add(map2);
        return list;
    }

    //根据年龄范围查询会员数量
    @Override
    public List<Map> getCountByAgeScope() {
        List<Map> list = new ArrayList<>();

        //0-18
        int count1 = memberMapper.findCountByAgeScope(0, 18);
        Map map1 = new HashMap<>();
        map1.put("name", "0-18");
        map1.put("value", count1);
        list.add(map1);

        //18-30
        int count2 = memberMapper.findCountByAgeScope(18, 30);
        Map map2 = new HashMap<>();
        map2.put("name", "18-30");
        map2.put("value", count2);
        list.add(map2);

        //30-45
        int count3 = memberMapper.findCountByAgeScope(30, 45);
        Map map3 = new HashMap<>();
        map3.put("name", "30-45");
        map3.put("value", count3);
        list.add(map3);

        //45以上
        int count4 = memberMapper.findCountByAgeScope(45, 120);
        Map map4 = new HashMap<>();
        map4.put("name", "45以上");
        map4.put("value", count4);
        list.add(map4);

        return list;
    }

    //根据日期范围查询会员每月新增数据
    @Override
    public Map getMemberReportByDate(List<Date> list) throws Exception {
        //实例化日历对象
        Calendar min = Calendar.getInstance();
        Calendar max = Calendar.getInstance();

        min.setTime(list.get(0));
        min.set(min.get(Calendar.YEAR), min.get(Calendar.MONTH), 1);

        max.setTime(list.get(1));
        max.set(max.get(Calendar.YEAR), max.get(Calendar.MONTH), 2);

        //定义一个list用来封装每一个月的月份
        List<String> memberMonths = new ArrayList<>();

        //定义一个list用来封装每一个月的会员数量
        List<Integer> memberCounts = new ArrayList<>();

        Calendar curr = min;
        while (curr.before(max)) {
            String date = DateUtils.parseDate2String(curr.getTime(),"yyyy-MM");
            memberMonths.add(date);
            System.out.println("date = " + date);
            //定义每个月查询的起始值
            String dateBegin = date + "-01";
            //定义每个月的结束值
            String dateEnd = date + "-31";

            //查询每个月的会员数量
            Integer memberCount = memberMapper.getMemberReport(dateBegin,dateEnd);
            memberCounts.add(memberCount);
            //当前月份加一
            curr.add(Calendar.MONTH, 1);
        }
        //将查询出来的数据封装到map中
        Map<String, List> map = new HashMap<>();
        map.put("memberMonths",memberMonths);
        map.put("memberCounts",memberCounts);

        return map;
    }

    public static void main(String[] args) throws Exception {
        //实例化日历对象
        Calendar calendar = Calendar.getInstance();
        //将日历对象的当前时间往前推12个月
        calendar.add(Calendar.MONTH,-12);

        //循环遍历，获取过去的十二个月
        for (int i = 0; i < 12; i++) {
            Date time = calendar.getTime();
            String date = DateUtils.parseDate2String(time,"yyyy-MM");
            System.out.println(date);

            calendar.add(Calendar.MONTH,1);
        }
    }
}
